package com.erp.client.widget;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.StatusButtonBar;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.erp.client.DAO.ServerUserBaseAsync;
import com.erp.client.DAO.SeriaRzLog;
import com.erp.client.MessAge.myMesswindow;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

public class myzModel extends Dialog {

    protected Button reset;
    protected Button save;
    protected FormPanel form;
    protected StatusButtonBar buttonBar;
    protected ServerUserBaseAsync customerService;
    protected BeanModel customerModel;
    protected ListStore<BeanModel> store;
    final private myMesswindow messdisp = new myMesswindow();

    public myzModel(final ServerUserBaseAsync customerService, final ListStore<BeanModel> store, final SeriaRzLog seriarzlog) {
        this.customerService = customerService;
        this.store = store;
        FormData formData = new FormData("100%");
        FormLayout layout = new FormLayout();
        layout.setLabelWidth(90);
        layout.setDefaultWidth(155);
        setLayout(layout);

        setButtons("");
        setIconStyle("user");
        setHeading(seriarzlog.getEmpName() + "的日志");//"我的日志");
        setModal(true);
        setBodyBorder(true);
        setBodyStyle("padding: 8px;background: none");
        setSize(500, 400);
        setResizable(false);

        form = new FormPanel();
        form.setHeading("我的日志");
        form.setHeaderVisible(false);
       



        final HiddenField<Integer> rzId = new HiddenField<Integer>();
        rzId.setName("rzId");
        rzId.setFieldLabel("rzId");
        // id.setVisible(false);
        form.add(rzId);

        final HiddenField<Integer> empId = new HiddenField<Integer>();
        empId.setName("empId");
        empId.setFieldLabel("empId");
        // id.setVisible(false);
        form.add(empId);

        final HiddenField<Integer> depId = new HiddenField<Integer>();
        depId.setName("depId");
        depId.setFieldLabel("depId");
        // id.setVisible(false);
        form.add(depId);

        final HiddenField<String> inputdate = new HiddenField<String>();
        inputdate.setName("inputDate");
        inputdate.setFieldLabel("inputDate");
        // id.setVisible(false);
        form.add(inputdate);

        final HiddenField<Integer> mangeId = new HiddenField<Integer>();
        mangeId.setName("mangeId");
        mangeId.setFieldLabel("mangeId");
        // id.setVisible(false);
        form.add(mangeId);

        final HiddenField<String> mangeDemo = new HiddenField<String>();
        mangeDemo.setName("mangeDemo");
        mangeDemo.setFieldLabel("mangeDemo");
        // id.setVisible(false);
        form.add(mangeDemo);

        final HiddenField<Double> mangeNumerical = new HiddenField<Double>();
        mangeNumerical.setName("mangeNumerical");
        mangeNumerical.setFieldLabel("mangeNumerical");
        // id.setVisible(false);
        form.add(mangeNumerical);

        final TextField<String> empname = new TextField<String>();
        empname.setName("empName");
        empname.setFieldLabel("姓名");
        empname.setAllowBlank(false);
        empname.setMinLength(2);
        empname.setMaxLength(10);
        empname.setReadOnly(true);
        empname.setEmptyText("请输入姓名");
        empname.setMessageTarget("姓名输入有错误！");

        form.add(empname);

        final TextField<String> depname = new TextField<String>();
        depname.setName("empName");
        depname.setFieldLabel("部门名称");
        depname.setAllowBlank(false);
        depname.setMaxLength(50);
        depname.setReadOnly(true);
        depname.setEmptyText("请输入部门名称");
        depname.setMessageTarget("部门名称输入有错误！");
        form.add(depname);

        final TextField<String> recdate = new TextField<String>();
        recdate.setName("recDate");
        recdate.setFieldLabel("日期");
        recdate.setAllowBlank(false);
        recdate.setMinLength(8);
        recdate.setMaxLength(12);
        recdate.setReadOnly(true);
        form.add(recdate);

        final TextField<String> week = new TextField<String>();
        week.setName("week");
        week.setFieldLabel("星期");
        week.setAllowBlank(false);
        //week.setMinLength(1);
        week.setMaxLength(8);
        week.setReadOnly(true);
        week.setEmptyText("请输入开始时间");
        week.setMessageTarget("请输入开始时间");
        form.add(week);

        final TextField<String> begintime = new TextField<String>();
        begintime.setName("beginTime");
        begintime.setFieldLabel("开始时间");
        begintime.setAllowBlank(false);
        begintime.setMinLength(5);
        begintime.setMaxLength(10);
        begintime.setEmptyText("请输入开始时间");
        begintime.setMessageTarget("请输入开始时间");
        
        form.add(begintime);
        final TextField<String> endtime = new TextField<String>();
        endtime.setName("endTime");
        endtime.setFieldLabel("结束时间");
        endtime.setAllowBlank(false);
        endtime.setMinLength(5);
        endtime.setMaxLength(10);
        endtime.setEmptyText("请输入结束时间");
        endtime.setMessageTarget("请输入日志内容");
        
        
        form.add(endtime);
        /*final TextField<String> recdata = new TextField<String>();
        recdata.setName("recData");
        recdata.setFieldLabel("内容");
        recdata.setAllowBlank(false);
        recdata.setMinLength(3);
        recdata.setMaxLength(500);
        recdata.setReadOnly(true);
        form.add(recdata);*/

        final TextArea recdata = new TextArea();
        recdata.setName("recData");
        recdata.setFieldLabel("内容");
        recdata.setAllowBlank(false);
        recdata.setMinLength(3);
        recdata.setMaxLength(500);
        recdata.setHeight(20);
        //recdata.setHideLabel(true);
        recdata.setEmptyText("请输入日志内容");
        recdata.setMessageTarget("请输入日志内容");
        
        form.add(recdata, new FormData("80% -73"));




        customerModel = BeanModelLookup.get().getFactory(SeriaRzLog.class).createModel(seriarzlog);
        final FormBinding formBinding = new FormBinding(form, true);
        formBinding.bind(customerModel);
        // formBinding.autoBind();
        // formBinding.addFieldBinding(new FieldBinding(age,"age"));

        add(form);
        buttonBar = new StatusButtonBar();
        setButtonBar(buttonBar);

        show();
    }

    @Override
    protected void createButtons() {
        reset = new Button("重置");
        reset.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                //myzModel.this.hide();
                form.reset();
            }
        });

        save = new Button("保存");

        save.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                if (form.isValid()) {
                   // buttonBar.getStatusBar().showBusy("正在处理，请稍后...");
                    //buttonBar.disable();

                    final SeriaRzLog customer = customerModel.getBean();
                    /*customerService = (ServerUserBaseAsync) GWT.create(ServerUserBase.class);
                    ServiceDefTarget endpoint = (ServiceDefTarget) customerService;
                    String moduleRelativeURL = GWT.getModuleBaseURL() + "dao/serveruserbase";
                    endpoint.setServiceEntryPoint(moduleRelativeURL);*/
                    customerService.UpdataRzlog(customer, new AsyncCallback() {

                        public void onFailure(Throwable caught) {
                            buttonBar.getStatusBar().hide();
                            messdisp.myMessWindow("提示信息", "保存失败！");
                            buttonBar.enable();
                        }

                        public void onSuccess(Object result) {
                            buttonBar.getStatusBar().hide();
                            store.add(customerModel);
                            store.getLoader().load();
                            //messdisp.myMessWindow("提示信息", (String) result);
                            buttonBar.enable();
                        }
                    });
                }
            }
        });

        buttonBar.add(save);
        buttonBar.add(reset);

    }
}
